/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.library;

import nemos.swing.IComponentFactory;
import nemos.swing.swixml.adapter.FactoryAdapter;

import org.apache.felix.scr.annotations.Component;
import org.osgi.framework.ServiceReference;
import org.swixml.Factory;
import org.swixml.SwingTagLibrary;
import org.swixml.TagLibrary;

/**
 * TagLibraryManager : The SwiXML tag library manager.
 * 
 * <p>
 * This component manages the SwiXML {@link TagLibrary}, providing it adapted
 * OSGi {@link IComponentFactory} services.
 * </p>
 * 
 * @author Romain DUBOIS
 */
@Component
public class TagLibraryManager extends AbstractLibraryManager<IComponentFactory, Factory, String> {

    /** The managed library */
    private final TagLibrary mLibrary;

    /**
     * Constructor
     */
    public TagLibraryManager() {
        super();

        mLibrary = SwingTagLibrary.getInstance();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#getCurrentService(java.lang.Object)
     */
    @Override
    protected Factory getCurrentService(final String pType) {
        return mLibrary.getFactory(pType);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#wrap(java.lang.Object,
     *      java.lang.Object)
     */
    @Override
    protected Factory wrap(@SuppressWarnings("unused") final String pType,
            final IComponentFactory pConverter) {
        return new FactoryAdapter(pConverter);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#register(java.lang.Object,
     *      java.lang.Object)
     */
    @Override
    protected void register(final String pType, final Factory s) {
        mLibrary.registerTag(pType, s);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#getConverterKey(org.osgi.framework.ServiceReference,
     *      java.lang.Object)
     */
    @Override
    protected String getConverterKey(final ServiceReference<IComponentFactory> pReference,
            @SuppressWarnings("unused") final IComponentFactory pConverter) {
        return (String) pReference.getProperty(IComponentFactory.COMPONENT_TYPE);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.swixml.library.AbstractLibraryManager#getConverterType()
     */
    @Override
    protected String getConverterType() {
        return IComponentFactory.class.getName();
    }
}
